#include "e2prom.h"
#include "ui_e2prom.h"
#include <QtWidgets/QPushButton>
#include <QtWidgets/QGridLayout>
#include <QMessageBox>
#include <QtWidgets/QDesktopWidget>
#include <stdio.h>
#include <stdlib.h>
#define COLUMN_SPACING 20
E2Prom::E2Prom(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::E2Prom)
{
    SetMainBacklight();


    ui->setupUi(this);
    ycapi = new Ycapi();
    ui->eepromEdit->setAttribute(Qt::WA_InputMethodEnabled,false);

}
void E2Prom::SetMainBacklight()
{
QPalette bgpal = palette();
bgpal.setColor (QPalette::Background, QColor (0, 0 , 0, 255));
bgpal.setColor (QPalette::Foreground, QColor (255,0,0,255));
setPalette (bgpal);
}
E2Prom::~E2Prom()
{
    delete ui;
}

void E2Prom::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void E2Prom::on_Get_clicked()
{
    char buf[256]={0};
    char *ptr=buf;
    ycapi->ReadEEPROM(0, ptr);
       printf("wwwwwwwwww is %s\n",ptr);
    ui->eepromEdit->setText(QString::fromLocal8Bit(ptr));
}

void E2Prom::on_Random_clicked()
{
    char buf[256]={0};
    char *ptr=buf;
    int i;
    for(i=0;i<10;i++)
    {
        buf[i]=random() % 26 +65;
    }
    ui->eepromEdit->setText(QString::fromLocal8Bit(ptr));
}

void E2Prom::on_Set_clicked()
{
    QString str;
    char *ptr;
    str=ui->eepromEdit->text();

    QByteArray ba =str.toUtf8();

    ptr= ba.data();

    ycapi->WriteEEPROM(0,ptr);
}

void E2Prom::on_close_clicked()
{
    this->close();
}


